<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button class="bg">开启</button>
    <button class="st">停止</button>
    <script>
        var bg = document.querySelector('.bg');
        var st = document.querySelector('.st');
        var timer = null;
        bg.addEventListener('click',function(){
            timer =  setInterval(function(){
                console.log('ni');
            },1000)
        })
        st.addEventListener('click',function(){
            clearInterval(timer);
        })
    </script>
</body>
</html>